package com.sailfish.springbootdemo.service.db1;

import com.sailfish.springbootdemo.dao.db1.*;
import com.sailfish.springbootdemo.pojo.Result;
import com.sailfish.springbootdemo.pojo.db1.*;
import com.sailfish.springbootdemo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class PcbaDeviceRelatedTechParamServiceImpl implements PcbaDeviceRelatedTechParamService {

    @Autowired
    private PcbaDeviceRelatedTechParamDao pcbaDeviceRelatedTechParamDao;

    @Autowired
    private TechDeviceParamDao techDeviceParamDao;

    @Autowired
    private PcbaDeviceResourceDao pcbaDeviceResourceDao;

    @Qualifier("MysqlTransactionManager1")
    @Autowired
    private DataSourceTransactionManager transactionManager1;

//    @Override
//    public List<PcbaDeviceRelatedTechParam> getAllByDeviceResourceId(Integer pcbaDeviceResourceId) {
//        List<PcbaDeviceRelatedTechParam> list = pcbaDeviceRelatedTechParamDao.getAllByDeviceResourceId(pcbaDeviceResourceId);
//        for (PcbaDeviceRelatedTechParam p : list) {
//            TechDeviceParam searchTechDeviceParam = new TechDeviceParam();
//            searchTechDeviceParam.setTechDeviceParamKey(p.getTechDeviceParamKey());
//            searchTechDeviceParam.setPcbaDeviceTypeId(p.getPcbaDeviceTypeId());
//            TechDeviceParam byTechDeviceParam = techDeviceParamDao.getByTechDeviceParam(searchTechDeviceParam);
//            if(byTechDeviceParam != null){
//                p.setTechDeviceParam(byTechDeviceParam);
//            }
//        }
//        return list;
//    }

    @Override
    public List<PcbaDeviceRelatedTechParam> getAllByDeviceResourceId(Integer pcbaDeviceResourceId) {
        List<PcbaDeviceRelatedTechParam> list = new ArrayList<>();
        PcbaDeviceResource deviceResource = pcbaDeviceResourceDao.getById(pcbaDeviceResourceId);
        if (deviceResource != null) {
            Integer pcbaDeviceTypeId = deviceResource.getPcbaDeviceTypeId();
            List<TechDeviceParam> allTechDeviceParam = techDeviceParamDao.getAllTechDeviceParam(pcbaDeviceTypeId, null, null);
            for (TechDeviceParam techDeviceParam : allTechDeviceParam) {
                PcbaDeviceRelatedTechParam pcbaDeviceRelatedTechParam = pcbaDeviceRelatedTechParamDao.getPcbaDeviceRelatedTechParamByKeyAndDeviceResourceId(techDeviceParam.getTechDeviceParamKey(), pcbaDeviceResourceId);
                if (pcbaDeviceRelatedTechParam != null) {
                    pcbaDeviceRelatedTechParam.setTechDeviceParam(techDeviceParam);
                    list.add(pcbaDeviceRelatedTechParam);
                } else {
                    PcbaDeviceRelatedTechParam p = new PcbaDeviceRelatedTechParam();
                    p.setTechDeviceParam(techDeviceParam);
                    p.setTechDeviceParamKey(techDeviceParam.getTechDeviceParamKey());
                    p.setTechDeviceParamValue("");
                    p.setPcbaDeviceResourceId(pcbaDeviceResourceId);
                    p.setPcbaDeviceTypeId(pcbaDeviceTypeId);
                    list.add(p);
                }
            }
        }
        return list;
    }

    @Override
    public Result addOrUpdateDeviceRelatedParam(PcbaDeviceRelatedTechParam pcbaDeviceRelatedTechParam, String mode) {
        try {
            if (pcbaDeviceRelatedTechParam == null) {
                return ResultUtil.error(500, "缺少参数pcbaDeviceRelatedTechParam", "Miss Param pcbaDeviceRelatedTechParam", null);
            }
            if (mode == null || "".equals(mode)) {
                return ResultUtil.error(500, "缺少参数mode", "Miss Param mode", null);
            }
            if (!("update".equals(mode) || "add".equals(mode))) {
                return ResultUtil.error(500, "mode 参数错误", "mode Param Error", null);
            }
            if ("update".equals(mode)) {
                if (pcbaDeviceRelatedTechParam.getPcbaDeviceRelatedTechParamId() == null || "".equals(pcbaDeviceRelatedTechParam.getPcbaDeviceRelatedTechParamId().toString())) {
                    return ResultUtil.error(500, "缺少参数pcbaDeviceRelatedTechParamId", "Miss Param 缺少参数pcbaDeviceRelatedTechParamId", null);
                }
                if (updateDeviceRelatedParam(pcbaDeviceRelatedTechParam)) {
                    return ResultUtil.success(null);
                }
                return ResultUtil.error(500, "操作失败", "Operate Fail", null);
            } else {
                if (addDeviceRelatedParam(pcbaDeviceRelatedTechParam)) {
                    return ResultUtil.success(null);
                }
                return ResultUtil.error(500, "操作失败", "Operate Fail", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public Boolean addDeviceRelatedParam(PcbaDeviceRelatedTechParam pcbaDeviceRelatedTechParam) {
        pcbaDeviceRelatedTechParam.setCreateTime(new Date());
        return pcbaDeviceRelatedTechParamDao.addDeviceRelatedParam(pcbaDeviceRelatedTechParam) > 0;
    }

    @Override
    public Boolean updateDeviceRelatedParam(PcbaDeviceRelatedTechParam pcbaDeviceRelatedTechParam) {
        pcbaDeviceRelatedTechParam.setUpdateTime(new Date());
        return pcbaDeviceRelatedTechParamDao.updateDeviceRelatedParam(pcbaDeviceRelatedTechParam) > 0;
    }

    @Override
    public PcbaDeviceRelatedTechParam getPcbaDeviceRelatedTechParamByResourceIdAndKey(Integer pcbaDeviceResourceId, String techDeviceParamKey) {
        return pcbaDeviceRelatedTechParamDao.getPcbaDeviceRelatedTechParamByKeyAndDeviceResourceId(techDeviceParamKey, pcbaDeviceResourceId);
    }
}
